# app/core/logging.py

import logging
import os
from datetime import datetime
from config.settings import settings

def setup_logging():
    """设置日志配置"""
    # 创建日志目录
    os.makedirs(os.path.dirname(settings.LOG_FILE), exist_ok=True)
    
    # 配置根日志记录器
    logging.basicConfig(
        level=getattr(logging, settings.LOG_LEVEL.upper()),
        format=settings.LOG_FORMAT,
        handlers=[
            logging.FileHandler(settings.LOG_FILE, encoding='utf-8'),
            logging.StreamHandler()
        ]
    )
    
    # 设置第三方库的日志级别
    logging.getLogger("uvicorn").setLevel(logging.INFO)
    logging.getLogger("celery").setLevel(logging.INFO)
    logging.getLogger("pymilvus").setLevel(logging.WARNING)

def get_logger(name: str) -> logging.Logger:
    """获取指定名称的日志记录器"""
    return logging.getLogger(name)

def log_api_request(logger: logging.Logger, method: str, path: str, status_code: int, duration: float):
    """记录API请求日志"""
    logger.info(
        f"API Request: {method} {path} - Status: {status_code} - Duration: {duration:.3f}s"
    )

def log_task_start(logger: logging.Logger, task_id: str, task_name: str):
    """记录任务开始日志"""
    logger.info(f"Task Started: {task_name} (ID: {task_id})")

def log_task_complete(logger: logging.Logger, task_id: str, task_name: str, duration: float):
    """记录任务完成日志"""
    logger.info(f"Task Completed: {task_name} (ID: {task_id}) - Duration: {duration:.3f}s")

def log_task_error(logger: logging.Logger, task_id: str, task_name: str, error: str):
    """记录任务错误日志"""
    logger.error(f"Task Error: {task_name} (ID: {task_id}) - Error: {error}")

def log_file_processing(logger: logging.Logger, file_path: str, file_type: str, status: str):
    """记录文件处理日志"""
    logger.info(f"File Processing: {file_path} ({file_type}) - Status: {status}")

def log_vectorization(logger: logging.Logger, model_name: str, input_count: int, duration: float):
    """记录向量化日志"""
    logger.info(f"Vectorization: {model_name} - Input: {input_count} - Duration: {duration:.3f}s") 